package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.bean.Item;
import com.xxxx.yoyo.bean.Order;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.IGoodsService;
import com.xxxx.yoyo.service.IItemService;
import com.xxxx.yoyo.service.IOrderService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.GoodsServiceImpl;
import com.xxxx.yoyo.service.impl.ItemServiceImpl;
import com.xxxx.yoyo.service.impl.OrderServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;

/**
 * Servlet implementation class PlaceOrederServlet
 */
@WebServlet("/index/placeOrder")
public class PlaceOrederServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ITypeService typeService = new TypeServiceImpl();
	private IOrderService orderService = new OrderServiceImpl();
	private IGoodsService goodsService = new GoodsServiceImpl();
	private IItemService itemService = new ItemServiceImpl();
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取session,查看session是否失效
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user == null) {//说明session失效了
			//重定向到登录页
			response.sendRedirect("/YoyoShop/index/login");
		}else {//session没有失效, session中的order可以正常获取
			//获取虚拟订单(里面有你要下单的宠物,总价,总数量等)
			Order order = (Order)session.getAttribute("order");
			
			//下单之前,检查每个宠物的库存是否不足.
			//因为你选购的时候,别人也在选购,当你下单的时候,可能别人已经下过单,有可能导致库存不足.
			
			//获取 购买项 列表
			List<Item> itemList = order.getItemList();
			//检查每个宠物库存是否不足
			for(int i = 0; i < itemList.size(); i++) {
				//获取购买项
				Item item = itemList.get(i);
				//获取购买数量
				int amount = item.getAmount();
				
				//获取宠物id
				int goodsId = item.getGoodId();
				//通过宠物id查询宠物,获取宠物的库存
				Goods goods = goodsService.getGoodsById(goodsId);
				int stock = goods.getStock();
				
				if(stock < amount) {//如果宠物的库存小于购买数量
					//提示错误信息
					request.setAttribute("msg", "下单失败!宠物[" + goods.getName() + "]库存不足,库存余量是:" + stock +"件");
					//请求转发到/index/cart页面,并且提示错误信息
					request.getRequestDispatcher("/index/cart").forward(request, response);
					return; //结束代码的执行
				}
			}
			
			//下单,把虚拟订单变成真实订单.
			order.setStatus(1);// 0表示删除，1表示未付款，2表示已付款，3表示配送中，4表示已完成
			order.setPaytype(0);//0表示未支付 1表示微信支付，2表示支付宝支付，3表示货到付款
			order.setSystime(new Date());//下单时间是此刻
			boolean isSuccess = orderService.addOrder(order);
			if(isSuccess) {//创建订单成功
				//获取订单的id
				int id = orderService.getIdByOrder(order);
				//把id设置给order对象
				order.setId(id);
				
				//根据订单信息往item表中插入购买的宠物
				itemService.addItemsByOrder(order);
				
				//下完单以后,应该清空购物车--把虚拟订单从session中删除
				session.removeAttribute("order");
				
				//把order存入request,方便/index/pay.jsp使用
				request.setAttribute("order", order);
				
				//获取 [宠物分类] 列表
				List<Type> typeList = typeService.getTypeList();
				//把typeList存入request中, 方便/index/pay.jsp页面取使用
				request.setAttribute("typeList", typeList);
				
				//请求转发,跳转到/index/pay.jsp页面
				request.getRequestDispatcher("/index/pay.jsp").forward(request, response);	
			}else {//创建订单失败
				//设置错误提示
				request.setAttribute("msg", "下单失败,创建订单出错!");
				//请求转发,跳转到购物车页面
				request.getRequestDispatcher("/index/cart").forward(request, response);
			}
		}
	}

}
